package vue_controleur;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Curseur extends Thread{
	private int _emplacement;
	private JTextPane _text;
	private Color _fond;
	private Color _carac;
	private StyledDocument _doc;
	private Boolean _change=true;
	private JFrame _frame;
	private Boolean _clic=false;
	
	public Curseur(int emplacement, JTextPane text, JFrame frame){
		_emplacement = emplacement;
		_text = text;
		_fond = _text.getBackground();
		_carac = _text.getForeground();		
		_doc = text.getStyledDocument();
		_frame = frame;
		Style style1 = text.addStyle("visible", null);
		StyleConstants.setForeground(style1, _carac);
		Style style2 = text.addStyle("invisible", null);
		StyleConstants.setForeground(style2, _fond);
	}
	public void run() {
		//boucle infini qui change la couleur du curseur 
		while(true){
			if(!_clic){
				if(_change){
					_doc.setCharacterAttributes(_emplacement, 1, _text.getStyle("visible"), true);
					_change=false;
				}else{
					_doc.setCharacterAttributes(_emplacement, 1, _text.getStyle("invisible"), true);
					_change=true;
				}
			}else{
				_doc.setCharacterAttributes(_emplacement, 1, _text.getStyle("visible"), true);
				_change=false;
			}
			try {
				sleep(700);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(!_frame.isActive()) break; //on stop le thread si la fenetre et ferme
		}
	}
	
	public void clicActif(){
		_clic=true;
	}
	public void clicDesactif(){
		_clic=false;
	}
	
	public void setEmplacement(int emplacement){
		_emplacement = emplacement;
		
	}
	public int getEmplacement(){
		return _emplacement;
	}
	public Boolean getClic(){
		return _clic;
	}
}
